﻿Imports System.Data.SqlClient
Public Class Frm_Dossier_Orig
    Public dv As Dossier
    Public avo As fichier_origin

    Private Sub Btn_parc_marji3ona_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Enabled = False
        Frm_Rech_Dossier.Show()
        Partager.type = "Frm_Dossier_Orig"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Enabled = False
        Frm_Mahkama.Show()
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        If Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر المرجع أولا", MsgBoxStyle.Critical, "خطأ في البحث")
        Else
            Me.Enabled = False
            Frm_Rech_Dossier_Orig.Show()
        End If
    End Sub

    Private Sub Frm_Dossier_Orig_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Partager.nonInitial = New ArrayList
            Partager.nonInitial.Add("Comb_marji3ona")

            Partager.com_ville(Comb_mahkama, "Comb_mahkama", "mahkama")
            Partager.close()
            Partager.remplirDossier(Comb_marji3ona)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click

        If (List_tous.SelectedIndices.Count > 0) Then
            MsgBox("عفوا لا يمكنك الإضافة، لإضافة ملف جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
        ElseIf Comb_marji3ona.SelectedIndex < 0 Then
            MsgBox("اختر الملف", MsgBoxStyle.Critical, "خطأ")
        Else
            If txt_num_milf.Text <> "" And txt_ra9m_hokm.Text <> "" And txt_mo9arir.Text <> "" Then
                If MsgBox("هل تريد فعلا إضافة هذاالملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة ملف") = MsgBoxResult.Yes Then
                    Partager.close()
                    Try
                        Dim cli As New fichier_origin(1, txt_num_milf.Text, txt_ra9m_hokm.Text, txt_hay2a.Text, Date_hokm.Value, txt_mo9arir.Text, Comb_mahkama.Text, txt_molahadat.Text)
                        dv.Ajouter_fichier_origine(cli)

                        List_tous.Items.Clear()
                        For Each khib As fichier_origin In dv.List_fichier_origine
                            List_tous.Items.Add(New ListViewItem(New String() {khib.Num, khib.Num_decision, khib.date_decision.Date, khib.tribunal, khib.autorie, khib.mo9arir}))
                        Next
                        MsgBox("تم الإضافة الملف بنجاح", MsgBoxStyle.Information, "نجاح")
                        chbOrigin.Checked = False
                    Catch sqlEx As SqlException
                        If sqlEx.Message.Contains("IX_Dossier_Original") Then
                            MsgBox("عفوا، هذا رقم هذا الملف استعمل من قبل، المرجو إدخال رقم ملف آخر", MsgBoxStyle.Critical, "خطأ")
                        Else
                            MsgBox(sqlEx.Message)
                        End If
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                End If
            Else
                MsgBox("أدخل المعلومات من فضلك", MsgBoxStyle.Critical, "خطأ")
            End If
        End If
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        If Comb_marji3ona.SelectedIndex >= 0 Then
            If List_tous.SelectedIndices.Count > 0 Then
                If txt_num_milf.Text <> "" And txt_ra9m_hokm.Text <> "" And txt_mo9arir.Text <> "" Then
                    Try
                        Partager.close()
                        Dim cli As New fichier_origin(1, txt_num_milf.Text, txt_ra9m_hokm.Text, txt_hay2a.Text, Date_hokm.Value, txt_mo9arir.Text, Comb_mahkama.Text, txt_molahadat.Text)
                        dv.modifier_List_fichier_origine(avo.Id, cli)
                        List_tous.Items.Clear()
                        For Each khib As fichier_origin In dv.List_fichier_origine
                            List_tous.Items.Add(New ListViewItem(New String() {khib.Num, khib.Num_decision, khib.date_decision.Date, khib.tribunal, khib.autorie, khib.mo9arir}))
                        Next
                        MsgBox("تم تعديل الملف بنجاح", MsgBoxStyle.Information, "نجاح")
                        Partager.vider(Me, Partager.nonInitial)
                    Catch sqlEx As SqlException
                        If sqlEx.Message.Contains("IX_Dossier_Original") Then
                            MsgBox("عفوا، هذا رقم هذا الملف استعمل من قبل، المرجو إدخال رقم ملف آخر", MsgBoxStyle.Critical, "خطأ")
                        Else
                            MsgBox(sqlEx.Message)
                        End If
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                Else
                    MsgBox("أدخل المعلومات من فضلك", MsgBoxStyle.Critical, "خطأ")
                End If
            Else
                MsgBox("اختر ملف من القائمة", MsgBoxStyle.Information, "خطأ في التعديل")
            End If
        Else
            MsgBox("اختر الملف", MsgBoxStyle.Information, "خطأ في التعديل")
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Partager.close()
        Try
            If List_tous.SelectedIndices.Count > 0 Then
                If MsgBox("هل تريد فعلا حذف هذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف ملف") = MsgBoxResult.Yes Then
                    Dim clt As fichier_origin = dv.List_fichier_origine(List_tous.SelectedIndices.Item(0))
                    dv.suprimmer_fichier_origine(clt.Id)

                    List_tous.Items.Clear()
                    For Each khib As fichier_origin In dv.List_fichier_origine
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Num, khib.Num_decision, khib.date_decision.Date, khib.tribunal, khib.autorie, khib.mo9arir}))
                    Next
                    MsgBox("تم حذف الملف بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me, Partager.nonInitial)
                End If
            Else
                MsgBox("اختر ملف من القائمة", MsgBoxStyle.Information, "خطأ")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            For Each i As Integer In List_tous.SelectedIndices
                avo = dv.List_fichier_origine(i)
                txt_num_milf.Text = avo.Num
                txt_ra9m_hokm.Text = avo.Num_decision
                txt_hay2a.Text = avo.autorie
                Date_hokm.Value = avo.date_decision
                txt_mo9arir.Text = avo.mo9arir
                Comb_mahkama.Text = avo.tribunal
                txt_molahadat.Text = avo.remarque
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        If List_tous.SelectedIndices.Count < 0 Then
            Partager.vider(Me)
        End If
    End Sub

   
    Public Sub remplir_champ(ByVal d As Dossier)
        Dim cmd As New SqlCommand("select ds.Num_Dossier," & _
        "ds.Tribunal," & _
        "dc.Num_Decision," & _
        "dc.Date_Decision," & _
        "mo.Nom From Dossier ds,Decision dc,Mo9arir mo where ds.Id = dc.Id_Dossier and ds.Id = mo.Id_Dossier and ds.Ref = ", Partager.con)
        Partager.open()
        Partager.dr = cmd.ExecuteReader
        If Partager.dr.HasRows Then
            Partager.dr.Read()
           
        End If
        Partager.dr.Close()
        Partager.close()

    End Sub

    Private Sub cmbOrigin_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            For Each d As Dossier In Partager.moi.List_info_fichier
                d.remplir_List_decisions()
                For Each dic As decisions In d.List_decisions
                    If dic.Type = "قطعي" Then
                        'If d.Ref.ToString = cmbOrigin.Text Then

                        '    txt_num_milf.Text = d.Num
                        '    Comb_mahkama.Text = d.Tribunal

                        '    Date_hokm.Value = dic.Date_decision
                        '    txt_ra9m_hokm.Text = dic.Num_decision

                        '    d.remplir_List_mo9arir()
                        '    For Each mo9 As M9arir In d.List_mo9arir
                        '        If mo9.actif Then
                        '            txt_mo9arir.Text = mo9.Nom
                        '        End If
                        '    Next
                        '    GoTo end_of_for
                        'End If
                    End If
                Next dic

            Next d
end_of_for:

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Public Sub remplireCombo2()
        Dim htClient As New Hashtable()
        Dim str As String = ""
        cmbOrigin.Items.Clear()

        For Each dss As Dossier In Partager.moi.List_info_fichier
            dss.remplir_List_decisions()
            For Each dic As decisions In dss.List_decisions
                If dic.Type = "قطعي" And dss.id <> CInt(Partager.deMarji.Key) And dv.Ref = dss.Ref Then

                    str = dss.Num & "  |  " & dss.Type_appel & "  |  " & dss.Tribunal
                    htClient.Add(dss.id, CStr(str))

                End If
            Next
        Next

        For Each ImageType As DictionaryEntry In htClient
            cmbOrigin.Items.Add(ImageType)
        Next
        cmbOrigin.DisplayMember = "value"
        cmbOrigin.ValueMember = "key"
        If cmbOrigin.Items.Count > 0 Then
            cmbOrigin.SelectedIndex = 0
        End If
    End Sub

    Private Sub chbOrigin_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbOrigin.CheckedChanged

        'Partager.nonInitial = New ArrayList
        'Partager.nonInitial.Add(cmbOrigin.Name)
        Partager.vider(Me, Partager.nonInitial)
        Try
            If chbOrigin.Checked Then
                If Comb_marji3ona.SelectedIndex >= 0 Then
                    cmbOrigin.Enabled = True
                    remplireCombo2()
                Else
                    MsgBox("اختر الملف أولا", MsgBoxStyle.Information, "خطأ")
                    chbOrigin.Checked = False
                End If
            Else
                cmbOrigin.Enabled = False
                cmbOrigin.Items.Clear()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me)
        chbOrigin.Checked = False
    End Sub

    Private Sub txt_num_milf_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_num_milf.KeyPress
        Dim allowedChars As String = "1234567890/"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub txt_ra9m_hokm_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_ra9m_hokm.KeyPress
        Dim allowedChars As String = "1234567890/"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub Comb_marji3ona_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        'Partager.nonInitial = New ArrayList
        'Partager.nonInitial.Add(cmbOrigin.Name)
        Partager.vider(Me, Partager.nonInitial)

        Try
            Partager.deMarji = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)
            For Each d As Dossier In Partager.moi.List_info_fichier
                If d.Archive = False Then
                    If d.id = CInt(Partager.deMarji.Key) Then
                        dv = d
                        dv.remplir_List_fichier_origine()
                    End If
                End If
            Next
            List_tous.Items.Clear()
            For Each khib As fichier_origin In dv.List_fichier_origine
                List_tous.Items.Add(New ListViewItem(New String() {khib.Num, khib.Num_decision, khib.date_decision.Date, khib.tribunal, khib.autorie, khib.mo9arir}))
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

  
    Private Sub Btn_Rech_Doss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Rech_Doss.Click
        Me.Enabled = False
        Partager.frmafect = Me
        Frm_Recherche.Show()
        Partager.type = Me.Name
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If chbOrigin.Checked Then
            Me.Enabled = False
            Partager.type = "Frm_Dossier_Orig"
            Frm_Rech_Dossier.Show()

        Else
            MsgBox("فعل الخاصية أولا", MsgBoxStyle.Critical, "خطأ")
        End If
    End Sub

    Private Sub cmbOrigin_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbOrigin.SelectedIndexChanged
        Try
            Partager.deOrigin = DirectCast(cmbOrigin.SelectedItem, DictionaryEntry)

            For Each d As Dossier In Partager.moi.List_info_fichier
                If d.id = Partager.deOrigin.Key Then
                    d.remplir_List_decisions()
                    For Each dic As decisions In d.List_decisions
                        If dic.Type = "قطعي" Then


                            txt_num_milf.Text = d.Num
                            Comb_mahkama.Text = d.Tribunal

                            Date_hokm.Value = dic.Date_decision
                            txt_ra9m_hokm.Text = dic.Num_decision

                            d.remplir_List_mo9arir()
                            For Each mo9 As M9arir In d.List_mo9arir
                                If mo9.actif Then
                                    txt_mo9arir.Text = mo9.Nom
                                End If
                            Next
                            GoTo end_of_for

                        End If
                    Next dic
                End If
            Next d
end_of_for:

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

   

End Class